import * as Cesium from "cesium";
import {getAssetsFile } from "../../../utils/utils"
/**
 * 添加动态线
 * @param pointList [jd,wd,jd,wd]   flat(3)
 * @param splitNum
 */
const addDynamicLine=(viewer:any,pointList: Array<any>)=> {
    //添加一个基础的底线
    viewer.entities.add({
        polyline: {
            positions: Cesium.Cartesian3.fromDegreesArray(pointList),
            width: 150,
            material: new Cesium.PolylineGlowMaterialProperty({
                glowPower: 0.005,
                taperPower: 1,
                color: Cesium.Color.fromCssColorString('#0abeff')
            }),
            clampToGround: true,
        },
    });
    //一个线的高亮动画，垂直方向，蓝色的线从无到有显示
    viewer.scene.groundPrimitives.add(
        new Cesium.GroundPolylinePrimitive({
            geometryInstances: new Cesium.GeometryInstance({
                geometry: new Cesium.GroundPolylineGeometry({
                    positions: Cesium.Cartesian3.fromDegreesArray(pointList),
                    width: 5.0,
                }),
            }),
            // appearance : new Cesium.PolylineColorAppearance()
            appearance: new Cesium.PolylineMaterialAppearance({
                material: new Cesium.Material({
                    fabric: {
                        type: 'DynamicColor',
                        uniforms: {
                            u_image: getAssetsFile('materialImg/GradientRed.png'),
                            u_color: Cesium.Color.fromCssColorString('#0abeff'),
                            u_speed: 2,
                        },
                        source: `
              czm_material czm_getMaterial(czm_materialInput materialInput) {
                  czm_material material = czm_getDefaultMaterial(materialInput);
                  vec2 st = materialInput.st * 2.;
                  vec4 colorImage = texture(u_image, vec2((1. - fract(st.s - u_speed * czm_frameNumber * 0.005)), st.t));
                  vec4 fragColor;
                  fragColor.rgb = u_color.rgb / 1.0;
                  fragColor = czm_gammaCorrect(fragColor); // 伽马校正
                  material.alpha = colorImage.a * u_color.a;
                  material.diffuse = u_color.rgb;
                  material.emission = fragColor.rgb;
                  return material;
              }
          `,
                    },
                }),
            }),
        })
    )
}
/**
 * 添加动态墙体
 * @param wallList   [jd,wd,jd,wd]  flat(3)
 * @param maximumHeights 最大高度
 * @param minimumHeights 最小高度
 */
const addWallGeojson=( viewer:any, {
        points=[],
        color='#3a5e88',
        alpha=0.8,
        maximumHeights=800,
        minimumHeights=0,
    }
)=> {
    let wallList=[Cesium.Cartesian3.fromDegreesArray(points)];
    const geometryInstances = wallList.map((positions) => {
        positions.push(positions[0])
        return new Cesium.GeometryInstance({
            geometry: new Cesium.WallGeometry({
                positions: positions,
                maximumHeights: positions.map(() => maximumHeights),
                minimumHeights: positions.map(() => minimumHeights),
                vertexFormat: Cesium.MaterialAppearance.VERTEX_FORMAT,
            }),
        })
    })
    const wallPrimitive = new Cesium.Primitive({
        geometryInstances,
        appearance: new Cesium.MaterialAppearance({
            material: new Cesium.Material({
                fabric: {
                    type: 'DynamicWall',
                    uniforms: {
                        image: getAssetsFile('materialImg/GradientRed.png'),
                        color: Cesium.Color.fromCssColorString(color || '#3a5e88'),
                        speed: 2,
                        alpha:alpha || 0.8
                    },
                    source: `
              czm_material czm_getMaterial(czm_materialInput materialInput) {
                  czm_material material = czm_getDefaultMaterial(materialInput);
                  vec2 st = materialInput.st;
                  vec4 colorImage = texture(image, vec2((1. - fract(st.t - speed * czm_frameNumber * 0.005)), st.t));
                  vec4 fragColor;
                  fragColor.rgb = color.rgb / 1.0;
                  fragColor = czm_gammaCorrect(fragColor); // 伽马校正
                  material.alpha = colorImage.a * color.a  * alpha;
                  material.diffuse = color.rgb;
                  material.emission = fragColor.rgb;
                  return material;
              }
          `,
                }
            }),
        }),
    })
    wallPrimitive.name = 'wall'
    viewer.scene.primitives.add(wallPrimitive)
}

/**
 * 添加遮罩区域，-这种适用于固定区域，固定行政区的,不需要看到全部的地图遮罩
 * @param viewer
 * @param points
 */
const addShadow=(viewer:any,points:Array<any>)=>{
    // 挖洞1-反选区域
    const entity = new Cesium.Entity({
        id: '2',
        polygon: {
            hierarchy: {
                // 绘制的区域太大容易卡顿
                positions: Cesium.Cartesian3.fromDegreesArray([70, 0, 70, 90, 140, 90, 140, 0]),
                holes: [
                    {
                        positions: Cesium.Cartesian3.fromDegreesArray(points),
                    },
                ]
            },
            material: Cesium.Color.fromCssColorString('#000000').withAlpha(0.4),
            zIndex: 2
        },
    })
    viewer.entities.add(entity)
}

export {
    addDynamicLine,
    addWallGeojson,
    addShadow
}


